//
//  LoginView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit
import MXCommonModule
import AuthenticationServices

protocol LoginDelegate: NSObjectProtocol {
    func loginWithEmail()
    func loginWithApple()
    func aboutTeam()
    func privacyPolicy()
}

class LoginView: UIView {

    weak var delegate: LoginDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoginView{
    @objc private func signWithEmailAction(){
        self.delegate?.loginWithEmail()
    }
    
    @objc func handleAuthorizationAppleIDButtonPress() {
        self.delegate?.loginWithApple()
    }
    
    @objc private func teamItemAction(){
        self.delegate?.aboutTeam()
    }
    
    @objc private func privacyPolicyAction(){
        self.delegate?.privacyPolicy()
    }
}

extension LoginView{
    private func createXglView(){
        let iconView = LoginUntil.creatLoginIconView()
        let emailItem = UIButton().then { make in
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.setTitle("Sign in with Email", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Semibold(fontSize: 18.0)
            make.addTarget(self, action: #selector(self.signWithEmailAction), for: .touchUpInside)
        }
        let appleItem = ASAuthorizationAppleIDButton(
            authorizationButtonType: .signIn,
            authorizationButtonStyle: .whiteOutline
        ).then { make in
            make.addTarget(self, action: #selector(self.handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
            make.cornerRadius = 24.0
        }
        let teamItem = BottomLineBtn().then { make in
            make.setTitle("Terms of Use", for: .normal)
            make.setTitleColor(UIColor.hexColor("#A2A2A2"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Semibold(fontSize: 15.0)
            make.addTarget(self, action: #selector(self.teamItemAction), for: .touchUpInside)
            make.lineColor = UIColor.hexColor("#A2A2A2")
        }
        let middleItem = UILabel().then { make in
            make.text = " & "
            make.textColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Semibold(fontSize: 15.0)
        }
        let pravicyItem = BottomLineBtn().then { make in
            make.setTitle("Privacy Policy", for: .normal)
            make.setTitleColor(UIColor.hexColor("#A2A2A2"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Semibold(fontSize: 15.0)
            make.addTarget(self, action: #selector(self.privacyPolicyAction), for: .touchUpInside)
            make.lineColor = UIColor.hexColor("#A2A2A2")
        }
        let bottomView = UIView()
        bottomView.addSubviews([teamItem,middleItem,pravicyItem])
        self.addSubviews([iconView,emailItem,appleItem,bottomView])
        iconView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 147.0))
            make.width.height.equalTo(conversionWithScrennWidth(value: 100.0))
        }
        emailItem.snp.makeConstraints { make in
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.centerX.equalToSuperview()
            make.top.equalTo(iconView.snp.bottom).offset(conversionWithScrennWidth(value: 180.0))
        }
        appleItem.snp.makeConstraints { make in
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.centerX.equalToSuperview()
            make.top.equalTo(emailItem.snp.bottom).offset(conversionWithScrennWidth(value: 33.0))
        }
        teamItem.snp.makeConstraints { make in
            make.left.centerY.height.equalToSuperview()
        }
        middleItem.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(teamItem.snp.right)
        }
        pravicyItem.snp.makeConstraints { make in
            make.centerY.height.equalToSuperview()
            make.left.equalTo(middleItem.snp.right)
        }
        bottomView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(appleItem.snp.bottom).offset(conversionWithScrennWidth(value: 50.0))
            make.height.equalTo(20.0)
            make.right.equalTo(pravicyItem)
        }
    }
}
